package edu.gduf.controller;

import edu.gduf.domain.Activity;
import edu.gduf.domain.ResultInfo;
import edu.gduf.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author 谢佳豪
 * @data 2020/10/9 - 17:29
 * @target 活动模块的控制类
 */
@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;


    /**
     * 跳转到活动广场
     * @param model
     * @return
     */
    @RequestMapping("/activitySquare")
    public String activitySquare(Model model){
        ResultInfo resultInfo = activityService.selectActivityList(1);
        model.addAttribute("resultInfo",resultInfo);
        model.addAttribute("currentPageIndex", 1);
        model.addAttribute("url","/activity/selectAll?currentPageIndex=");
        return "activity/activitySquare";
    }

    /**
     * 跳转到发布活动页面
     *
     * @return publishActivity页面
     */
    @RequestMapping("/publishActivity")
    public String publishActivity(Model model) {
        ResultInfo resultInfo = activityService.catalogList();
        model.addAttribute("catalogList", resultInfo.getData());
        return "activity/publishActivity";
    }

    /**
     * 跳转到更新活动页面
     * @param id 活动id
     * @param model
     * @return
     */
    @RequestMapping("/updateActivity")
    public String updateActivity(Integer id, Model model){
        ResultInfo resultInfo = activityService.selectActivityById(id);
        model.addAttribute("activity",resultInfo.getData());
        model.addAttribute("catalogList", activityService.catalogList().getData());
        return "activity/updateActivity";
    }

    /**
     * 活动发布
     *
     * @param activity    前端表单提交activity数据
     * @param httpSession httpSession域
     * @return activity页面
     */
    @RequestMapping(value = "/publish", method = RequestMethod.POST)
    public String publish(Activity activity, HttpSession httpSession, Model model) {
        ResultInfo resultInfo = activityService.addActivity(activity, httpSession);
        if (resultInfo.getStatus() == 0) {
            model.addAttribute("msg", resultInfo.getMessage());
            return "activity/publishActivity";
        } else {
            model.addAttribute("msg", resultInfo.getMessage());
            return "forward:/activity/activitySquare";
        }
    }

    /**
     * 获取活动目录
     * @return
     */
    @RequestMapping("/getCatalogList")
    @ResponseBody
    public ResultInfo getCatalogList(){
        return activityService.catalogList();
    }

    /**
     * 活动查询（全部-分页）
     *
     * @param currentPageIndex 当前页面下标值
     * @return
     */
    @RequestMapping(value = "/selectAll", method = RequestMethod.GET)
    public String selectAll(Integer currentPageIndex, Model model) {
        ResultInfo resultInfo = activityService.selectActivityList(currentPageIndex);
        model.addAttribute("resultInfo",resultInfo);
        model.addAttribute("currentPageIndex", currentPageIndex);
        model.addAttribute("url","/activity/selectAll?currentPageIndex=");
        return "activity/activitySquare";
    }

    /**
     * 活动查询（条件-分页）
     *
     * @param cid              分类目录的id
     * @param currentPageIndex 当前页面下标值
     * @return json
     */
    @RequestMapping(value = "/selectActivities", method = RequestMethod.GET)
    public String selectActivities(Integer cid, Integer currentPageIndex, Model model) {
        ResultInfo resultInfo = activityService.selectActivitiesByCid(cid, currentPageIndex);
        model.addAttribute("resultInfo", resultInfo);
        model.addAttribute("cid", cid);
        model.addAttribute("currentPageIndex", currentPageIndex);
        model.addAttribute("url","/activity/selectActivities?");
        return "activity/activitySquare";
    }

    /**
     * 根据activity_id查询单条活动详细信息--同时更新view浏览量
     *
     * @param id activity表主键id
     * @return activityContent页面
     */
    @RequestMapping(value = "/selectActivityById", method = RequestMethod.GET)
    public String selectActivityById(Integer id, Model model) {
        ResultInfo resultInfo = activityService.selectActivityById(id);
        model.addAttribute("activity", resultInfo.getData());
        return "activity/activityContent";
    }

    /**
     * 根据组织id查询其所拥有的活动信息
     *
     * @param currentPageIndex 当前页面下标
     * @return checkPublish页面
     */
    @GetMapping("/selectActivitiesByAid")
    public String selectActivitiesByAid(Integer currentPageIndex, Model model, HttpSession httpSession) {
        Integer aid = (Integer) httpSession.getAttribute("uid");
        ResultInfo resultInfo = activityService.selectActivitiesByAid(aid, currentPageIndex);
        model.addAttribute("resultInfo", resultInfo);
        model.addAttribute("currentPageIndex", currentPageIndex);
        model.addAttribute("url","/activity/selectActivitiesByAid?");
        return "user/showrelease";
    }

    /**
     * 根据title模糊查询活动信息数据
     *
     * @param title 标题信息
     * @return
     */
    @RequestMapping("/selectActivitiesByTitle")
    public String selectActivitiesByTitle(String title, Integer currentPageIndex, Model model) {
        ResultInfo resultInfo = activityService.selectActivitiesByTitle(title, currentPageIndex);
        model.addAttribute("resultInfo", resultInfo);
        model.addAttribute("currentPageIndex", currentPageIndex);
        model.addAttribute("url","/activity/selectActivitiesByTitle?");
        return "activity/activitySquare";
    }

    /**
     * 活动更新
     *
     * @param activity 活动对象信息
     * @return json
     */
    @PostMapping("/update")
    public String update(Activity activity, Model model, RedirectAttributes attributes) {
        ResultInfo resultInfo = activityService.updateActivity(activity);
        model.addAttribute("msg", resultInfo.getMessage());
        attributes.addAttribute("currentPageIndex", 1);
        return "redirect:/activity/selectActivitiesByAid";
    }

    /**
     * 根据activity_id 删除活动信息
     *
     * @param id activity_id
     * @return json
     */
    @RequestMapping("/delete")
    public String delete(Integer id, RedirectAttributes attributes) {
        activityService.delete(id);
        attributes.addAttribute("currentPageIndex", 1);
        return "redirect:/activity/selectActivitiesByAid";
    }

    /**
     * 根据uid查询主题数
     * @param uid
     * @return
     */
    @ResponseBody
    @RequestMapping("/searchNum")
    public ResultInfo searchNum(Integer uid){
        return activityService.searchNum(uid);
    }


    /**
     * 选择合适的用户，减少招募人数需求
     *
     * @param id activity_id
     * @return json
     */
    @RequestMapping("/choose")
    @ResponseBody
    public ResultInfo reducePopulation(Integer id, String username) {
        return activityService.reducePopulation(id, username);
    }

    /**
     * 根据发布时间和结束时间的时间段，判断是否时间到了，进行修改活动状态
     *
     * @param timeout 时间标识符
     * @param id      activity_id
     * @return json
     */
    @RequestMapping("/isTimeout")
    public ResultInfo updateStatus(Boolean timeout, Integer id) {
        return activityService.updateStatus(timeout, id);
    }
}
